﻿namespace MSSqlSchemaDoc.Provider.MicrosoftSqlServer
{
    using System.Collections.ObjectModel;
    using System.Data;
    using MSSqlSchemaDoc.Core.DataStructures;

    /// <summary>
    /// Contains scripting helper methods for indexes.
    /// </summary>
    public partial class SqlScriptingManager
    {
        /// <summary>
        /// Adds index data to a table.
        /// </summary>
        /// <param name="indexObject">The table for which to add the indexes.</param>
        /// <param name="indexData">The index data for the table or for all tables.</param>
        private void AddIndexes(IIndexableDataObject indexObject, DataTable indexData)
        {
            indexObject.Indexes = new Collection<Index>();

            // Use a while-loop since we need to increment the loop counter manually
            int i = 0;
            while (i < indexData.Rows.Count)
            {
                int indexId = indexData.Rows[i].Field<int>("ObjectId");
                Index index = null;

                while (i < indexData.Rows.Count
                    && indexObject.ObjectId == indexData.Rows[i].Field<int>("TableId")
                    && indexId == indexData.Rows[i].Field<int>("ObjectId"))
                {
                    DataRow indexRow = indexData.Rows[i];

                    if (index == null)
                    {
                        index = new Index();

                        index.Name = indexRow.Field<string>("ObjectName");
                        index.IndexType = indexRow.Field<string>("IndexTypeDescription");
                        index.IsUnique = indexRow.Field<bool>("IsUnique");
                        index.IgnoreDuplicateKey = indexRow.Field<bool>("IgnoreDuplicateKey");
                        index.IsPrimaryKey = indexRow.Field<bool>("IsPrimaryKey");
                        index.IsUniqueConstraint = indexRow.Field<bool>("IsUniqueConstraint");
                        index.FillFactor = indexRow.Field<byte>("FillFactor");
                        index.IsPadded = indexRow.Field<bool>("IsPadded");
                        index.IsDisabled = indexRow.Field<bool>("IsDisabled");
                        index.IsHypothetical = indexRow.Field<bool>("IsHypothetical");
                        index.AllowRowLocks = indexRow.Field<bool>("AllowRowLocks");
                        index.AllowPageLocks = indexRow.Field<bool>("AllowPageLocks");
                        index.HasFilter = indexRow.Field<bool>("HasFilter");

                        string filterDefinition = indexRow.Field<string>("FilterDefinition");
                        index.FilterDefinition = string.IsNullOrEmpty(filterDefinition) ? string.Empty : filterDefinition;

                        index.Columns = new Collection<IndexColumn>();

                        indexObject.Indexes.Add(index);
                    }

                    IndexColumn col = new IndexColumn();
                    col.Name = indexRow.Field<string>("ColumnName");
                    col.IsDecendingKey = indexRow.Field<bool>("IsDecendingKey");
                    col.IsIncludedColumn = indexRow.Field<bool>("IsIncludedColumn");

                    index.Columns.Add(col);

                    i++;
                }

                // If the index is not initialized, then we need to increment the counter,
                // otherwise it should already be incremented.
                if (index == null)
                {
                    i++;
                }
            }
        }
    }
}
